/*
 * Copyright 2010. 
 * 
 * This document may not be reproduced, distributed or used 
 * in any manner whatsoever without the expressed written 
 * permission of xrkol Corp. 
 * 
 * $Rev: Rev $
 * $Author: Author $
 * $LastChangedDate: LastChangedDate $
 *
 */

package com.xrkol.sharelib.service.impl;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xrkol.sharelib.dao.AttendanceDao;
import com.xrkol.sharelib.dao.GuardianDao;
import com.xrkol.sharelib.dao.StudentDao;
import com.xrkol.sharelib.entity.Attendance;
import com.xrkol.sharelib.entity.Clazz;
import com.xrkol.sharelib.entity.Guardian;
import com.xrkol.sharelib.entity.Student;
import com.xrkol.sharelib.service.StudentService;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

/**
 * @author peng.xia
 *
 */
@Service
@Transactional
public class StudentServiceImpl implements StudentService{

    @Autowired
    private StudentDao studentDao;
    
    @Autowired
    private GuardianDao guardianDao;
    
    @Autowired
    private AttendanceDao attendanceDao;
    
    @Override
    public void save(Student entity) {
        this.studentDao.save(entity);
    }

    @Override
    public void update(Student entity) {
        this.studentDao.update(entity);
    }

    @Override
    public void delete(String id) {
        this.studentDao.delete(id);
    }

    @Override
    public Student findById(String id) {
        return this.studentDao.findById(id);
    }

    @Override
    public List<Student> findStudentsWithPagination(Integer page) {
        return this.studentDao.findStudentsWithPagination(page);
    }

    @Override
    public List<Student> findByUserId(String userId) {
        List<Guardian> guardians=this.guardianDao.findByUserId(userId);
        List<Student> students=Lists.newArrayList();
        for (Guardian guardian : guardians) {
            students.add(guardian.getStudent());
        }
        return students;
    }

    @Override
    public Set<Student> getStudentsByClass(Clazz clazz) {
        List<Attendance> attendances=this.attendanceDao.getStudentsByClass(clazz);
        Set<Student> students=Sets.newLinkedHashSet();
        for (Attendance attendance: attendances) {
            students.add(attendance.getStudent());
        }
        return students;
    }
    
    @Override
    public List<Student> getStudentsByClasses(List<Clazz> clazzs) {
        List<Student> students = Lists.newArrayList();
        for (Clazz clazz : clazzs) {
            students.addAll(getStudentsByClass(clazz));
        }
        return students;
    }
    
    @Override
    public List<Student> getNoChoiceStudents() {
        List<Student> students=this.studentDao.getAllStudents();
        List<Student> results=Lists.newArrayList();
        results.addAll(students);
        List<Attendance> attendStudents=this.attendanceDao.getAll();
        for (Student student : students) {
            for (Attendance attendance : attendStudents) {
                if(attendance.getStudent()==student){
                    results.remove(student);
                }
            }
        }
        return results;
    }
}
